package com.westcom.vulb.activity;

import org.json.JSONArray;

import com.westcom.vulb.logic.SessionManager;
import com.westcom.vulb.logic.account;
import com.westcom.vulb.logic.sheet;
import com.westcom.vulb.protocal.http.Response;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.view.View;
import android.view.View.*;

public class CreateSheetActivity extends BasicActivity 
{
	EditText sheetNameTextView, confirmCodeTextView;                            
	RadioGroup mRadioGroup;       
	RadioButton notShareRadioButton, shareRadioButton;
	Button seleceMemberBtn;
	
	TextView rightHeaderDone,backBtn;
	SessionManager sm;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sheet_create);
        
        sm = SessionManager.getInstance();
        
        OnClickListener mclicklistener = new OnClickListener()
        {
			@Override
			public void onClick(View v) 
			{
				// TODO Auto-generated method stub
				switch(v.getId())
				{
					case R.id.member_select:
					{
						Intent intent = new Intent();   
						intent.putExtra("sheetName",sheetNameTextView.getText().toString()); 
				        intent.setClass(CreateSheetActivity.this, CreateSheetAddShareMemberActivity.class);
				        startActivity(intent);					
					}break;
					case R.id.create_sheet_right:
					{
						CreateNotShareSheetTask task = new CreateNotShareSheetTask();
						String [] d_t = new String [1];
						d_t[0] = sheetNameTextView.getText().toString();
                		task.execute(d_t);
						
					}break;
					case R.id.create_sheet_left:
					{
				        finish();
						
					}break;
					
					default:{}break;
				}
			}
			
		};
		
		rightHeaderDone = (TextView) findViewById(R.id.create_sheet_right);
        rightHeaderDone.setOnClickListener(mclicklistener);
        
        seleceMemberBtn = (Button)this.findViewById(R.id.member_select);
        seleceMemberBtn.setOnClickListener(mclicklistener);
        
        backBtn = (TextView)this.findViewById(R.id.create_sheet_left);
        backBtn.setOnClickListener(mclicklistener);
        
        sheetNameTextView = (EditText)this.findViewById(R.id.et_sheet_name);
        confirmCodeTextView = (EditText)this.findViewById(R.id.et_confirm_code);
        
        mRadioGroup = (RadioGroup)this.findViewById(R.id.radioGroup);
        notShareRadioButton = (RadioButton)this.findViewById(R.id.radio_notshare);
        shareRadioButton = (RadioButton)this.findViewById(R.id.radio_share);             

        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        	public void onCheckedChanged(RadioGroup group, int checkedId) {
            	if (checkedId == shareRadioButton.getId()) {
            		seleceMemberBtn.setVisibility(View.VISIBLE);
            		confirmCodeTextView.setVisibility(View.VISIBLE);
                } else {
                	seleceMemberBtn.setVisibility(View.GONE);
            		confirmCodeTextView.setVisibility(View.GONE);
                }
            }
        });  
    }
    private class CreateNotShareSheetTask extends AsyncTask<String, Integer, Integer> {
    	String[] IdsTodel = null;
        @Override
        protected Integer doInBackground(String... params) {
        	IdsTodel = new String[params.length];
        	for(int i = 0;i<params.length;i++)
        	{
        		IdsTodel[i] = params[i];
        	}
        	Response sheet_res;
        	sheet_res = sheet.getInstanse().CreateSheet(sm.getCurrentSession().getSessionid(), IdsTodel[0], false, confirmCodeTextView.getText().toString(), "");
        	if(sheet_res == null)
        		return null;
        	try
        	{
        		if(sheet_res.errno == Response.SUCCESS)
            	{
        			
            	}
        	}catch (Exception e) 
        	{
        		
        	}
        	 return null;
        }
        
        @Override
        protected void onCancelled() {
            super.onCancelled();
            mLoadingDialog.hide();
        }
        
        @Override  
        protected void onPostExecute(Integer result) 
        {  
        	super.onPostExecute(result);
        	if(result == null){
        		Intent intent = new Intent();   
		        intent.setClass(CreateSheetActivity.this, SheetBrowseActivity.class);
		        startActivity(intent);
        	}else{
        		launchToast(result);
        	}
        	mLoadingDialog.hide();
        }
    }
}
